<?php
include ("globals/isLoanPending.inc");

get_post_ifset("hull_upgrade, engine_upgrade, power_upgrade, fighter_upgrade, sensors_upgrade, beams_upgrade, armor_upgrade, cloak_upgrade, ecm_upgrade, torp_launchers_upgrade, shields_upgrade, total_cost, total_cost2");

if ($playerinfo['turns'] < 1)
{
	echo "$l_trade_turnneed<BR><BR>";
}
else
{
	$trade_ore		= round(abs($trade_ore));
	$trade_organics = round(abs($trade_organics));
	$trade_goods	= round(abs($trade_goods));
	$trade_energy	 = round(abs($trade_energy));

	if ($sectorinfo['port_type'] == "spacedock")
	{

		if (isLoanPending($playerinfo['player_id']))
		{
			echo "$l_port_loannotrade<p>";
			echo "<A HREF=igb.php>$l_igb_term</a><p>";
			echo $l_global_mmenu;
			include ("footer.php");
			die();
		}

		$hull_upgrade_cost = 0;
		if($shipinfo['hull'] != $shipinfo['hull_normal']){
			if ($hull_upgrade > $shipinfo['hull_normal'])
				$hull_upgrade = $shipinfo['hull_normal'];

			if ($hull_upgrade < 0)
				$hull_upgrade = 0;

			if ($hull_upgrade > $shipinfo['hull'])
			{
				$hull_upgrade_cost = phpChangeDelta($hull_upgrade, $shipinfo['hull']);
			}
		}
		else
		{
			$hull_upgrade = $shipinfo['hull'];
		}

		$engine_upgrade_cost = 0;
		if($shipinfo['engines'] != $shipinfo['engines_normal']){
			if ($engine_upgrade > $shipinfo['engines_normal'])
				$engine_upgrade = $shipinfo['engines_normal'];

			if ($engine_upgrade < 0)
				$engine_upgrade = 0;

			if ($engine_upgrade > $shipinfo['engines'])
			{
				$engine_upgrade_cost = phpChangeDelta($engine_upgrade, $shipinfo['engines']);
			}
		}
		else
		{
			$engine_upgrade = $shipinfo['engines'];
		}

		$power_upgrade_cost = 0;
		if($shipinfo['power'] != $shipinfo['power_normal']){
			if ($power_upgrade > $shipinfo['power_normal'])
				$power_upgrade = $shipinfo['power_normal'];

			if ($power_upgrade < 0)
				$power_upgrade = 0;

			if ($power_upgrade > $shipinfo['power'])
			{
				$power_upgrade_cost = phpChangeDelta($power_upgrade, $shipinfo['power']);
			}
		}
		else
		{
			$power_upgrade = $shipinfo['power'];
		}

		$fighter_upgrade_cost = 0;
		if($shipinfo['fighter'] != $shipinfo['fighter_normal']){
			if ($fighter_upgrade > $shipinfo['fighter_normal'])
				$fighter_upgrade = $shipinfo['fighter_normal'];

			if ($fighter_upgrade < 0)
				$fighter_upgrade = 0;

			if ($fighter_upgrade > $shipinfo['fighter'])
			{
				$fighter_upgrade_cost = phpChangeDelta($fighter_upgrade, $shipinfo['fighter']);
			}
		}
		else
		{
			$fighter_upgrade = $shipinfo['fighter'];
		}

		$sensors_upgrade_cost = 0;
		if($shipinfo['sensors'] != $shipinfo['sensors_normal']){
			if ($sensors_upgrade > $shipinfo['sensors_normal'])
				$sensors_upgrade = $shipinfo['sensors_normal'];

			if ($sensors_upgrade < 0)
				$sensors_upgrade = 0;

			if ($sensors_upgrade > $shipinfo['sensors'])
			{
				$sensors_upgrade_cost = phpChangeDelta($sensors_upgrade, $shipinfo['sensors']);
			}
		}
		else
		{
			$sensors_upgrade = $shipinfo['sensors'];
		}

		$beams_upgrade_cost = 0;
		if($shipinfo['beams'] != $shipinfo['beams_normal']){
			if ($beams_upgrade > $shipinfo['beams_normal'])
				$beams_upgrade = $shipinfo['beams_normal'];

			if ($beams_upgrade < 0)
				$beams_upgrade = 0;

			if ($beams_upgrade > $shipinfo['beams'])
			{
				$beams_upgrade_cost = phpChangeDelta($beams_upgrade, $shipinfo['beams']);
			}
		}
		else
		{
			$beams_upgrade = $shipinfo['beams'];
		}

		$armor_upgrade_cost = 0;
		if($shipinfo['armor'] != $shipinfo['armor_normal']){
			if ($armor_upgrade > $shipinfo['armor_normal'])
				$armor_upgrade = $shipinfo['armor_normal'];

			if ($armor_upgrade < 0)
				$armor_upgrade = 0;

			if ($armor_upgrade > $shipinfo['armor'])
			{
				$armor_upgrade_cost = phpChangeDelta($armor_upgrade, $shipinfo['armor']);
			}
		}
		else
		{
			$armor_upgrade = $shipinfo['armor'];
		}

		$cloak_upgrade_cost = 0;
		if($shipinfo['cloak'] != $shipinfo['cloak_normal']){
			if ($cloak_upgrade > $shipinfo['cloak_normal'])
				$cloak_upgrade = $shipinfo['cloak_normal'];

			if ($cloak_upgrade < 0)
				$cloak_upgrade = 0;

			if ($cloak_upgrade > $shipinfo['cloak'])
			{
				$cloak_upgrade_cost = phpChangeDelta($cloak_upgrade, $shipinfo['cloak']);
			}
		}
		else
		{
			$cloak_upgradegrade = $shipinfo['cloak'];
		}

		$torp_launchers_upgrade_cost = 0;
		if($shipinfo['torp_launchers'] != $shipinfo['torp_launchers_normal']){
			if ($torp_launchers_upgrade > $shipinfo['torp_launchers_normal'])
				$torp_launchers_upgrade = $shipinfo['torp_launchers_normal'];

			if ($torp_launchers_upgrade < 0)
				$torp_launchers_upgrade = 0;

			if ($torp_launchers_upgrade > $shipinfo['torp_launchers'])
			{
				$torp_launchers_upgrade_cost = phpChangeDelta($torp_launchers_upgrade, $shipinfo['torp_launchers']);
			}
		}
		else
		{
			$torp_launchers_upgrade = $shipinfo['torp_launchers'];
		}

		$shields_upgrade_cost = 0;
		if($shipinfo['shields'] != $shipinfo['shields_normal']){
			if ($shields_upgrade > $shipinfo['shields_normal'])
				$shields_upgrade = $shipinfo['shields_normal'];

			if ($shields_upgrade < 0)
				$shields_upgrade = 0;

			if ($shields_upgrade > $shipinfo['shields'])
			{
				$shields_upgrade_cost = phpChangeDelta($shields_upgrade, $shipinfo['shields']);
			}
		}
		else
		{
			$shields_upgrade = $shipinfo['shields'];
		}

		$ecm_upgrade_cost = 0;
		if($shipinfo['ecm'] != $shipinfo['ecm_normal']){
			if ($ecm_upgrade > $shipinfo['ecm_normal'])
				$ecm_upgrade = $shipinfo['ecm_normal'];

			if ($ecm_upgrade < 0)
				$ecm_upgrade = 0;

			if ($ecm_upgrade > $shipinfo['ecm'])
			{
				$ecm_upgrade_cost = phpChangeDelta($ecm_upgrade, $shipinfo['ecm']);
			}
		}
		else
		{
			$ecm_upgrade = $shipinfo['ecm'];
		}

		$total_cost = $hull_upgrade_cost + $engine_upgrade_cost + $power_upgrade_cost + $fighter_upgrade_cost +
						$sensors_upgrade_cost + $beams_upgrade_cost + $armor_upgrade_cost + $cloak_upgrade_cost +
						$torp_launchers_upgrade_cost + $shields_upgrade_cost + $ecm_upgrade_cost;

		$total_cost = round($total_cost * $alliancefactor * ($repair_modifier / 100));

		if ($total_cost > $playerinfo['credits'])
		{
			echo "$l_ports_needcredits " . NUMBER($total_cost) . " $l_ports_needcredits1 " . NUMBER($playerinfo['credits']) . " $l_credits.";
		}
		else
		{
			$trade_credits = NUMBER(abs($total_cost));
			echo "<TABLE BORDER=2 CELLSPACING=2 CELLPADDING=2 BGCOLOR=#400040 WIDTH=600 ALIGN=CENTER>
			 <TR>
				<TD colspan=99 align=center bgcolor=#300030><font size=3 color=white><b>$l_trade_result</b></font></TD>
			 </TR>
			 <TR>
				<TD colspan=99 align=center><b><font color=red>$l_cost : " . $trade_credits . " $l_credits</font></b></TD>
			 </TR>";

			//	Total cost is " . NUMBER(abs($total_cost)) . " credits.<BR><BR>";
			$debug_query = $db->Execute("UPDATE {$db_prefix}players SET credits=credits-$total_cost,turns=turns-1, turns_used=turns_used+1 WHERE player_id=$playerinfo[player_id]");
			db_op_result($debug_query,__LINE__,__FILE__);

			$query = "UPDATE {$db_prefix}ships SET class=$shipinfo[class] ";

			if ($hull_upgrade > $shipinfo['hull'])
			{
				$query = $query . ", hull=$hull_upgrade";
				BuildOneCol("$l_hull $l_trade_upgraded $hull_upgrade");
			}
			if ($engine_upgrade > $shipinfo['engines'])
			{
				$query = $query . ", engines=$engine_upgrade";
				BuildOneCol("$l_engines $l_trade_upgraded $engine_upgrade");
			}
			if ($power_upgrade > $shipinfo['power'])
			{
				$query = $query . ", power=$power_upgrade";
				BuildOneCol("$l_power $l_trade_upgraded $power_upgrade");
			}
			if ($fighter_upgrade > $shipinfo['fighter'])
			{
				$query = $query . ", fighter=$fighter_upgrade";
				BuildOneCol("$l_fighter $l_trade_upgraded $fighter_upgrade");
			}
			if ($sensors_upgrade > $shipinfo['sensors'])
			{
				$query = $query . ", sensors=$sensors_upgrade";
				BuildOneCol("$l_sensors $l_trade_upgraded $sensors_upgrade");
			}
			if ($beams_upgrade > $shipinfo['beams'])
			{
				$query = $query . ", beams=$beams_upgrade";
				BuildOneCol("$l_beams $l_trade_upgraded $beams_upgrade");
			}
			if ($armor_upgrade > $shipinfo['armor'])
			{
				$query = $query . ", armor=$armor_upgrade";
				BuildOneCol("$l_armor $l_trade_upgraded $armor_upgrade");
			}
			if ($cloak_upgrade > $shipinfo['cloak'])
			{
				$query = $query . ", cloak=$cloak_upgrade";
				BuildOneCol("$l_cloak $l_trade_upgraded $cloak_upgrade");
			}
			if ($torp_launchers_upgrade > $shipinfo['torp_launchers'])
			{
				$query = $query . ", torp_launchers=$torp_launchers_upgrade";
				BuildOneCol("$l_torp_launch $l_trade_upgraded $torp_launchers_upgrade");
			}
			if ($shields_upgrade > $shipinfo['shields'])
			{
				$query = $query . ", shields=$shields_upgrade";
				BuildOneCol("$l_shields $l_trade_upgraded $shields_upgrade");
			}
			if ($ecm_upgrade > $shipinfo['ecm'])
			{
				$query = $query . ", ecm=$ecm_upgrade";
				BuildOneCol("$l_ecm $l_trade_upgraded $ecm_upgrade");
			}

			$query = $query . " WHERE ship_id=$shipinfo[ship_id]";
			$debug_query = $db->Execute("$query");
			db_op_result($debug_query,__LINE__,__FILE__);

			$hull_upgrade=0;
			echo "
			</table>
			";
		}
		echo "<BR><BR> <A HREF=port.php>$l_clickme</A> $l_port_returntospecial";
	}
}

//-------------------------------------------------------------------------------------------------

echo "<BR><BR>";
echo $l_global_mmenu;

include ("footer.php");

?>
